package com.salinity.kun.task;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.salinity.kun.helper.BaseHydrologyDataHelper;
import com.salinity.kun.helper.OracleHelper;
import com.salinity.kun.model.StationDateData;
import com.salinity.kun.util.DateUtil;

public class DenseInterpolationTask implements ITask {

	public DenseInterpolationTask() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean run() throws IOException, ParseException {
		int interval = 5;
		String dateStr = "2018/06/1 0:0:0";
		String stationID = "2c9240214f50ba3a0150655a06be000c";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Path outputPath = Paths.get("E:\\Kuner\\BYLW\\Jworkspace\\saltyCode\\test\\");
		int shortInterval = 1;
		Date SDate = sdf.parse(dateStr);
		Date EDate = DateUtil.LDTime2Date(DateUtil.Date2LDTime(SDate).plusDays(3));
		
		OracleHelper oracleHelper = new OracleHelper();
		ResultSet rsltSet = null;
		List<StationDateData> hydDataList = new ArrayList<>();
		try {
			oracleHelper.prepareTideSQL();
			
			rsltSet = oracleHelper.doTideQuery(SDate, EDate, stationID);
			hydDataList = oracleHelper.parseRslt(rsltSet);
			oracleHelper.end();
		} catch (SQLException e) {
			 
			e.printStackTrace();
		}
		
		BaseHydrologyDataHelper.writeToFile(hydDataList, outputPath.resolve("rawData.csv"));
		
		//do linear interpolation
		List<StationDateData> linearHydDataList = BaseHydrologyDataHelper.doLinearInterpolation(hydDataList, SDate, EDate, interval);
		BaseHydrologyDataHelper.writeToFile(linearHydDataList, outputPath.resolve("linear_5min.csv"));
		// do dense interpolation
		List<StationDateData> denseHydDataList = BaseHydrologyDataHelper.doProportionalInterpolation(linearHydDataList, interval, interval/shortInterval);
		
		BaseHydrologyDataHelper.writeToFile(denseHydDataList, outputPath.resolve("dense_1min.csv"));
		
		return false;
	}
	
	public static void main(String[] args) {
		DenseInterpolationTask denseTask = new DenseInterpolationTask();
		try {
			denseTask.run();
		} catch (IOException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
